package com.lx.errorHanding.retry;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

/**
 * @author lixing
 * @date 2021/7/13 14:38
 */
@Component
public class RetryItemProcessor implements ItemProcessor<String, String> {
    private int attempCount = 0;

    @Override
    public String process(String s) throws Exception {
        System.out.println("processor item is " + s);

        if (s.equalsIgnoreCase("23")) {
            attempCount++;

            if (attempCount >= 3) {
                System.out.println("Retired" + attempCount + "time success");
                return String.valueOf(Integer.valueOf(s) * -1);
            } else {
                System.out.println("Retired" + attempCount + "time fail");
                throw new CustomRetryableException("Process failed Attempt:" + attempCount);
            }
        } else {
            return String.valueOf(Integer.valueOf(s) * -1);
        }
    }
}
